package com.sssre.lttcloud.auth.controller;

import com.sssre.lttcloud.auth.mapper.UserMapper;
import com.sssre.lttcloud.common.result.Result;
import com.sssre.lttcloud.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/injection")
    public Result testInjection() {
        try {
            // 测试UserMapper注入
            if (userMapper != null) {
                System.out.println("UserMapper注入成功: " + userMapper.getClass().getName());
            } else {
                return Result.failed("UserMapper注入失败");
            }

            // 测试RedisUtils注入
            if (redisUtil != null) {
                System.out.println("RedisUtils注入成功: " + redisUtil.getClass().getName());
                
                // 测试Redis连接
                boolean redisTest = redisUtil.set("test:injection", "success", 60);
                if (redisTest) {
                    Object value = redisUtil.get("test:injection");
                    System.out.println("Redis测试成功: " + value);
                } else {
                    return Result.failed("Redis连接测试失败");
                }
            } else {
                return Result.failed("RedisUtils注入失败");
            }

            return Result.success("所有依赖注入测试成功");
        } catch (Exception e) {
            return Result.failed("测试失败: " + e.getMessage());
        }
    }

    @GetMapping("/user-count")
    public Result getUserCount() {
        try {
            long count = userMapper.selectCount(null);
            return Result.success("用户总数: " + count);
        } catch (Exception e) {
            return Result.failed("查询用户数量失败: " + e.getMessage());
        }
    }
} 